<template>
  <div id="app">
    <a-input  placeholder="请输入任务" class="my_ipt" :value="inputVal" @change="handleInputChange" />
    <a-button type="primary" @click="addItem">添加事项</a-button>
    <a-list bordered :dataSource="viewUi" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框  change事件只能传递e在函数体可以往外传-->
        <a-checkbox :checked="item.done" @change="(e)=>{cbStatusChanged(e,item.id)}">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="deleteItem(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneLength}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="viewKey==='all'?'primary':'default'" @click="changeList('all')">全部</a-button>
          <a-button :type="viewKey==='undone'?'primary':'default'" @click="changeList('undone')">未完成</a-button>
          <a-button :type="viewKey==='done'?'primary':'default'" @click="changeList('done')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clean">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'app',
  data () {
    return {
    }
  },
  created () {
    this.$store.dispatch('listStore/getList')
  },
  computed: {
    /** 方式1 */
    // list () {
    //   console.log(this.$store.state.listStore.list)
    //   return this.$store.state.listStore.list
    // }

    /** 方式2 */
    // 如果去的和key值一致，可以省略成['list']
    ...mapState({
      list: state => state.listStore.list,
      inputVal: state => state.listStore.inputVal,
      viewKey: state => state.listStore.viewKey
    }),

    /** 方式1 */
    // unDoneLength () {
    //    console.log(this.$store.getters['listStore/unDoneLength'])
    //    return this.$store.getters['listStore/unDoneLength']
    // },
    /** 方式2 */
    ...mapGetters({
      unDoneLength: 'listStore/unDoneLength',
      viewUi: 'listStore/viewUi'
    })
  },
  methods: {
    handleInputChange (e) {
      // console.log(e.target.value)
      this.$store.commit('listStore/setInputVal', e.target.value)
    },
    addItem () {
      if (this.inputVal.trim().length <= 0) {
        return this.$message.warning('文本框内容不能为空')
      }
      this.$store.commit('listStore/addItem')
    },
    deleteItem (id) {
      this.$store.commit('listStore/deleteItem', id)
    },
    cbStatusChanged (e, id) {
      // console.log(e.target.checked, id)
      const param = {
        id: id,
        status: e.target.checked
      }
      this.$store.commit('listStore/changeStatus', param)
    },
    clean () {
      this.$store.commit('listStore/cleanDone')
    },
    changeList (key) {
      this.$store.commit('listStore/changeViewKey', key)
    }
  }

}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
